﻿using DLLPC.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VisionCore
{
    public class Status_ExcSTFlatnesCheck : ProcedureHelper_Exc
    {
        string subStatusCode = "";
        string tempStatusCode = "";
        bool condition = false;
        bool start = false;

        SystemTimeCounter scStatus = new SystemTimeCounter();
        public void Main()
        {
            condition =Exc_MS_Up && Exc_Up && (ExcTR_X_AtFlip|| ExcTR_X_ULD) && Kernel.motion.ExcST_Y.IsHomeAlready && Kernel.motion.ExcST_R.IsHomeAlready
                     && Kernel.motion.ExcTR_X.IsHomeAlready && Kernel.motion.ExcTR_R.IsHomeAlready && Kernel.motion.ExcTR_Z.IsHomeAlready && Door_Lock;
            start = condition && ManualMode && false;

            switch (ExcST_FlatnesCheck_StatusCode)
            {
                case FlatnesCheckStatus.Init:
                    subStatusCode = "00: 初始化";
                    {
                        if (start)
                        {
                            ExcST_FlatnesCheck_StatusCode = FlatnesCheckStatus.CheckStatus;
                        }
                    }
                    break;
                case FlatnesCheckStatus.CheckStatus:
                    subStatusCode = "05: 状态判断";
                    {
                        ExcST_FlatnesCheck_StatusCode = FlatnesCheckStatus.ToCheckPos;
                        scStatus.StatusStart();
                    }
                    break;
                case FlatnesCheckStatus.ToCheckPos:
                    subStatusCode = "10: YR移动到倒膜位";
                    {
                        if (ExcST_Exchange)
                        {
                            ExcST_FlatnesCheck_StatusCode = FlatnesCheckStatus.Cylinder_ToDown;
                            scStatus.Start();   
                        }
                        else if (scStatus.Duration>5000)
                        {
                            break;
                        }
                    }
                    break;
                case FlatnesCheckStatus.Cylinder_ToDown:
                    subStatusCode = "11: 倒膜气缸下降";
                    {
                        if (Exc_Down && Exc_MS_Down)
                        {
                            ExcST_FlatnesCheck_StatusCode = FlatnesCheckStatus.ReadValue;
                            scStatus.Start();
                        }
                        else if (scStatus.Duration > 2000)
                        {
                            break;
                        }
                    }
                    break;
                case FlatnesCheckStatus.ReadValue:
                    subStatusCode = "12: 读取测量值";
                    {
                        if (true)
                        {
                            ExcST_FlatnesCheck_StatusCode = FlatnesCheckStatus.Cylinder_ToUp;
                        }
                        else if (scStatus.Duration > 2000)
                        {
                            break;
                        }
                    }
                    break;
                case FlatnesCheckStatus.Cylinder_ToUp:
                    subStatusCode = "13: 倒膜气缸上升";
                    {
                        if (Exc_Up && Exc_MS_Up)
                        {
                            ExcST_FlatnesCheck_StatusCode = FlatnesCheckStatus.ToNext;
                        }
                        else if (scStatus.Duration > 2000)
                        {
                            break;
                        }
                    }
                    break;
                case FlatnesCheckStatus.ToNext:
                    subStatusCode = "14: 下一测量位";
                    {
                        ExcST_FlatnesCheck_StatusCode = FlatnesCheckStatus.ShowValues;
                    }
                    break;
                case FlatnesCheckStatus.ShowValues:
                    subStatusCode = "20: 显示测量结果";
                    {
                        ExcST_FlatnesCheck_StatusCode = FlatnesCheckStatus.CheckComplete;
                    }
                    break;
                case FlatnesCheckStatus.CheckComplete:
                    subStatusCode = "21: 检查完成";
                    {
                        ExcST_FlatnesCheck_StatusCode = FlatnesCheckStatus.Init;
                    }
                    break;
                    default:
                    break;
            }

            LogStatusCode(subStatusCode);
        }

        private void LogStatusCode(string msg)
        {
            if (msg != tempStatusCode)
            {
                tempStatusCode = msg;
                LogMsg("倒膜平台平整度确认- " + tempStatusCode);
            }
        }
    }
}
